package organizer.main;

import java.awt.GridBagConstraints;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;

import organizer.ui.UI;

public class Organizer {
	public static void main(String[] args) {
		Organizer organizer = new Organizer();
		UI ui = new UI(organizer);
	}

	// private static Organizer instance = new Organizer();

	private GregorianCalendar gcal = null;

	private GridBagConstraints gbc = null;
	private int weekOfYear = 0;
	private Vector<Day> days = null;

	private UI ui = null;

	// public static void setInstance(Organizer instance) {
	// Organizer.instance = instance;
	// }

	// public static Organizer getInstance() {
	// return instance == null ? (instance = new Organizer()) : instance;
	// }

	public GregorianCalendar getGcal() {
		return gcal;
	}

	public void setGcal(GregorianCalendar gcal) {
		this.gcal = gcal;
	}

	public int getWeekOfYear() {
		return weekOfYear;
	}

	public void setWeekOfYear(int weekOfYear) {
		this.weekOfYear = weekOfYear;
	}

	public Vector<Day> getDays() {
		return days;
	}

	public void setDays(Vector<Day> days) {
		this.days = days;
	}

	private Organizer() {
		gcal = new GregorianCalendar();
		gbc = new GridBagConstraints();

		days = new Vector<Day>(40);

//		Date today = new Date();
//		gcal.setTime(today);
//		System.out.println("Today: " + today);
//
//		loadWeek(gcal.get(Calendar.WEEK_OF_YEAR));
	}

	public void loadWeek(int week) {
		System.out.println(weekOfYear + " -> Loading KW " + week);
		weekOfYear = week; // gcal.get(Calendar.WEEK_OF_YEAR);
		gcal.set(Calendar.WEEK_OF_YEAR, week);

		int month = gcal.get(Calendar.MONTH);
		int maxDay = gcal.getActualMaximum(Calendar.DAY_OF_MONTH);
		System.out.println("Max Day: " + maxDay);

		int dayOfWeek = gcal.get(Calendar.DAY_OF_WEEK);
		int daysToAdd = maxDay;

		// get to beginning of month
		gcal.set(Calendar.DAY_OF_MONTH, 1);
		System.out.println("Set gcal time to: " + gcal.getTime());
		daysToAdd += getDaysUntil(Calendar.MONDAY, false);
		Date dateToStartFrom = gcal.getTime();

		// restliche Tage bis folgenden Sonntag holen...
		gcal.set(Calendar.MONTH, month);
		gcal.set(Calendar.DAY_OF_MONTH, maxDay);
		System.out.println("Set gcal time to: " + gcal.getTime());
		daysToAdd += getDaysUntil(Calendar.SUNDAY, true);

		// reset gcal
		gcal.setTime(dateToStartFrom);
		for (int i = 0; i < daysToAdd; i++) {
			// System.out.println(dayOfWeek);
			Day day = new Day(gcal.getTime());
			days.add(day);

			// System.out.println("Adding date " + gcal.getTime());
			gcal.add(Calendar.DAY_OF_WEEK, 1);
			// dayOfWeek = gcal.get(Calendar.DAY_OF_WEEK);
		}
	}

	private int getDaysUntil(int calendarDay, boolean increaseDate) {
		int operator = 1;
		if (!increaseDate) {
			operator *= -1;
		}
		// System.out.println("Going back to " + (increaseDate ? "next" :
		// "last")
		// + " " + calendarDay);
		int retValue = 0;
		while (gcal.get(Calendar.DAY_OF_WEEK) != calendarDay) {
			// increase ammount of days to add later
			retValue++;

			gcal.add(Calendar.DAY_OF_WEEK, operator);
			// System.out.println("Went to " + gcal.getTime());
		}
		return retValue;
	}
}
